package com.hitqz.robot.biz.schedule;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.cache.Cache;
import com.google.common.collect.Lists;
import com.hitqz.robot.api.business.dto.*;
import com.hitqz.robot.api.business.entity.PatrolResultEntity;
import com.hitqz.robot.api.business.enums.RobotModelEnum;
import com.hitqz.robot.api.business.enums.RobotOtherCommandEnum;
import com.hitqz.robot.api.business.enums.TicketsStatusEnum;
import com.hitqz.robot.api.business.enums.WorkStatusEnum;
import com.hitqz.robot.api.common.dto.tickets.ParseAgvRobotParams;
import com.hitqz.robot.api.common.entity.*;
import com.hitqz.robot.api.common.enums.SubscribeTopicEnum;
import com.hitqz.robot.api.dispatch.protocol.ProtocolEnum;
import com.hitqz.robot.biz.config.GuavaCacheConfig;
import com.hitqz.robot.biz.service.*;
import com.hitqz.robot.biz.util.PatrolConstant;
import com.hitqz.robot.biz.websocket.util.BroadcastTopicUtil;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.driver.dto.robot.RobotCustomInfo;
import com.hitqz.robot.driver.model.IRobotPosDto;
import com.hitqz.robot.driver.model.PointDto;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.hitqz.robot.netty.service.Send2ServerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.operation.distance.DistanceOp;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author xupkun
 * @date 2024/6/21
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class WsSendStatusSchedule {

    private final RobotService robotService;

    private final RobotDriverService robotDriverService;

    private final Send2ServerService send2ServerService;

    private final SysWarningService sysWarningService;

    private final TicketsService ticketsService;

    private final EquipmentFactory equipmentFactory;

    private final TicketsTaskService ticketsTaskService;

    private final RobotEquipmentService robotEquipmentService;

    private final ButtonService buttonService;
    private final RobotMapService robotMapService;

    private final PatrolResultService patrolResultService;

    private final Cache<String, Boolean> robotWebRefreshCache;

    private static final DecimalFormat df = new DecimalFormat("0.00");
    private final Cache<String, Boolean> remoteControlAuthCache;
    private final RobotConfigService robotConfigService;
    private final RobotMapPosService robotMapPosService;

    @Scheduled(fixedDelay = 500L)
    public void sendStatus() {
        IRobotPosDto iRobotPosDto = robotDriverService.getWebPos();
        //log.info("webPos:" + JSONObject.toJSONString(iRobotPosDto));
        if (iRobotPosDto != null) {
            Robot robot = robotService.getDefaultRobot();
            RobotClientSummaryDto dto = new RobotClientSummaryDto();
            //检测报警
            dto.setVisionWarningStatus((PatrolConstant.visionWaringFlag.get() && PatrolConstant.webWarningStatus.get()) ? 1 : 0);
            //外设
            RobotEquipmentPanelDto robotEquipmentPanelDto = new RobotEquipmentPanelDto();
            List<RobotEquipment> list = robotEquipmentService.list(Wrappers.<RobotEquipment>lambdaQuery().eq(RobotEquipment::getIsPlay, 1));
            if (list != null && !list.isEmpty()) {
                //预览地址
                robotEquipmentPanelDto.setPlayUrls(list.stream().map(RobotEquipment::getPlayUrl).filter(StrUtil::isNotBlank).toList());
            }
            //外设按钮
            robotEquipmentPanelDto.setPanels(robotEquipmentService.getControlButtonList());
            dto.setEquipmentPanel(robotEquipmentPanelDto);
            RobotStatusDto robotStatusDto = new RobotStatusDto();
            dto.setClientId(robot.getRobotSn());
            robotStatusDto.setName(robot.getRobotName());
            robotStatusDto.setSn(robot.getRobotSn());
            robotStatusDto.setIp(robot.getIpAddress());
            robotStatusDto.setOnlineStatus(Integer.valueOf(robot.getOnlineStatus()));
            robotStatusDto.setOnlineStatusStr(robot.getOnlineStatus().equals("1") ? "在线" : "离线");
            robotStatusDto.setErrorStatus(1);
            robotStatusDto.setErrorStatusStr("正常");
            robotStatusDto.setBattery(String.valueOf(iRobotPosDto.getBatteryLevel()));
            robotStatusDto.setVoltage(String.valueOf(iRobotPosDto.getVoltage()));
            robotStatusDto.setCurrent(String.valueOf(iRobotPosDto.getCurrent()));
            robotStatusDto.setCurrentDirection(iRobotPosDto.getCurrentDirection() == null ? "放电" : iRobotPosDto.getCurrentDirection());
            robotStatusDto.setChargeStatus(iRobotPosDto.isCharging() ? 1 : 0);
            robotStatusDto.setChargeStatusStr(iRobotPosDto.isCharging() ? "充电中" : "正常速度控制");
            robotStatusDto.setTaskStatus(iRobotPosDto.getTaskStatus());
            robotStatusDto.setTaskStatusStr(iRobotPosDto.getTaskStatusStr());
            robotStatusDto.setTemperature(String.valueOf(iRobotPosDto.getTemperature()));

            robotStatusDto.setSpeed(df.format(iRobotPosDto.getSpeed()));
            robotStatusDto.setWorkStatus(Integer.parseInt(robot.getWorkStatus()));
            robotStatusDto.setWorkStatusStr(WorkStatusEnum.CMD_ROBOT_STATUS.get(robot.getWorkStatus()).getDesc());
            if (Objects.equals(robot.getRobotModel(), RobotModelEnum.PATROL.getFlag())) {
                robotStatusDto.setWorkStatusStr(robotStatusDto.getWorkStatusStr().replace("作业中", "巡检中"));
            }
            if (Objects.equals(robotStatusDto.getChargeStatusStr(), "充电中") && Objects.equals(robotStatusDto.getWorkStatusStr(), "空闲")) {
                robotStatusDto.setWorkStatusStr("充电中");
            }
            if (Objects.equals(robotStatusDto.getWorkStatusStr(), "空闲")) {
                robotStatusDto.setWorkStatusStr("待机中");
            }
            robotStatusDto.setEmergencyStop(iRobotPosDto.isEmergency() ? 1 : 0);
            robotStatusDto.setEmergencyStopStr(iRobotPosDto.isEmergency() ? "急停触发" : "未触发");
            //语音通话请求
            String selfSpeakStatus = Optional.ofNullable(robotDriverService.executeOtherCommand(RobotOtherCommandEnum.GET_SPEAK_REQUEST, "")).map(R::getData).map(Object::toString).orElse("0");
            boolean speakStatus = PatrolConstant.padVoiceRequest.get() == 1 || Objects.equals(selfSpeakStatus, "1");
            robotStatusDto.setSpeakStatus(speakStatus ? "1" : "0");

            Boolean remoteControlAuthDto = remoteControlAuthCache.getIfPresent(GuavaCacheConfig.REMOTE_CONTROL_AUTH);
            if (remoteControlAuthDto != null && remoteControlAuthDto) {
                robotStatusDto.setRemoteControlStatus(1);
                robotStatusDto.setRemoteControlStatusStr("已控制");
            } else {
                robotStatusDto.setRemoteControlStatus(0);
                robotStatusDto.setRemoteControlStatusStr("未控制");
            }
            dto.setStatus(robotStatusDto);


            RobotPositionDto robotPositionDto = new RobotPositionDto();
            robotPositionDto.setRobotIcon(robotConfigService.getRobotIcon());
            robotPositionDto.setMapCode(robot.getMapCode());
            robotPositionDto.setUrl(iRobotPosDto.getUrl());
            robotPositionDto.setWebControlSpeed(iRobotPosDto.getWebControlSpeed());
            RobotMap map = robotMapService.findByMapCode(robot.getMapCode());
            robotPositionDto.setWebRotation(map.getWebRotation());
            /**
             * 机器人像素位置
             */
            RobotLaserPosDto robotPixelPos = new RobotLaserPosDto();
            robotPixelPos.setYaw(Double.parseDouble(String.valueOf(iRobotPosDto.getPixelAngle())));
            robotPixelPos.setX(Double.parseDouble(String.valueOf(iRobotPosDto.getPixelX())));
            robotPixelPos.setY(Double.parseDouble(String.valueOf(iRobotPosDto.getPixelY())));
            robotPixelPos.setZ(0.0D);
            robotPositionDto.setPixelPos(robotPixelPos);

            /**
             * 机器人形式过的路径
             */
            ArrayList<RobotPosDto> passedPath = new ArrayList<>();
            for (PointDto pointDto : CollUtil.emptyIfNull(iRobotPosDto.getPassedPath())) {
                RobotPosDto robotPosDto = new RobotPosDto();
                robotPosDto.setX(pointDto.getX());
                robotPosDto.setY(pointDto.getY());
                passedPath.add(robotPosDto);
            }
            robotPositionDto.setPassedPath(passedPath);


            /**
             * 机器人激光位置
             */
            RobotLaserPosDto robotLaserPosDto = new RobotLaserPosDto();
            robotLaserPosDto.setYaw(Double.parseDouble(String.valueOf(iRobotPosDto.getAngle())));
            robotLaserPosDto.setX(Double.parseDouble(String.valueOf(iRobotPosDto.getX())));
            robotLaserPosDto.setY(Double.parseDouble(String.valueOf(iRobotPosDto.getY())));
            robotLaserPosDto.setZ(0.0D);

            String currentStation = iRobotPosDto.getCurrentStation();
            if (StringUtils.isBlank(currentStation)) {
                List<RobotMapPos> currentMapPositions = robotMapPosService.list(Wrappers.<RobotMapPos>lambdaQuery()
                        .eq(RobotMapPos::getMapCode, robot.getMapCode()));
                try {
                    RobotMapPos nearestPosition = findNearestPosition(iRobotPosDto, currentMapPositions);
                    currentStation = nearestPosition.getCode();
                } catch (Exception e) {
                    log.error("获取当前位置失败{}", e.getMessage());
                }
            }
            robotLaserPosDto.setNearPosCode(currentStation);
            robotLaserPosDto.setIsReached(iRobotPosDto.isReached());
            robotPositionDto.setLaserPos(robotLaserPosDto);


            /**
             * 二维码位置
             */
            RobotQRPosDto robotQRPosDto = new RobotQRPosDto();
            robotQRPosDto.setLabel("");
            robotQRPosDto.setAngle(0.0D);
            robotQRPosDto.setX(0.0D);
            robotQRPosDto.setY(0.0D);
            robotQRPosDto.setZ(0.0D);
            robotPositionDto.setQrCodePos(robotQRPosDto);
            RobotPosDto robotPosDto = new RobotPosDto();
            robotPosDto.setX(Double.parseDouble(String.valueOf(iRobotPosDto.getX())));
            robotPosDto.setY(Double.parseDouble(String.valueOf(iRobotPosDto.getY())));
            robotPosDto.setZ(0.0D);
            robotPositionDto.setMileage(robotPosDto);
            robotPositionDto.setLaser2D(Lists.newArrayList());
            robotPositionDto.setLaser3D("");
            dto.setPosition(robotPositionDto);
            RobotTaskDto robotTaskDto = new RobotTaskDto();
            RobotTransportTaskDto robotTransportTaskDto = new RobotTransportTaskDto();
            RobotPatrolTaskDto robotPatrolTaskDto = new RobotPatrolTaskDto();
            List<Tickets> lastTenTickets = ticketsService.list(Wrappers.<Tickets>lambdaQuery()
                    .isNotNull(Tickets::getServerId)
                    .orderByDesc(Tickets::getId)
                    .last("limit 10"));
            robotTaskDto.setLastTenServerIds(lastTenTickets.stream().map(Tickets::getServerId).toList());

            if (robot.getWorkStatus().equals(WorkStatusEnum.WORK.getCode())) {
                //
                Tickets tickets = ticketsService.getOne(Wrappers.<Tickets>lambdaQuery()
                        .eq(Tickets::getStatus, TicketsStatusEnum.STATUS_EXECUTING.getCode())
                        .orderByDesc(Tickets::getId)
                        .last("limit 1"));
                if (tickets != null) {
                    robotTaskDto.setId(String.valueOf(tickets.getId()));
                    robotTaskDto.setServerId(tickets.getServerId() == null ? null : String.valueOf(tickets.getServerId()));
                    robotTaskDto.setName(tickets.getName());
                    robotTaskDto.setStartTime(DateUtil.format(tickets.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
                    if (robot.getRobotModel().equals(RobotModelEnum.AGV.getFlag()) && StrUtil.isNotBlank(tickets.getContent())) {
                        robotTaskDto.setType("3");
                        TicketsTask ticketsTask = ticketsTaskService.getOne(Wrappers.<TicketsTask>lambdaQuery()
                                .eq(TicketsTask::getTicketsId, tickets.getId())
                                .eq(TicketsTask::getStatus, TicketsStatusEnum.TICKETS_TASK_EXECUTING.getCode()).last(" limit 1")
                        );
                        ParseAgvRobotParams params = JSONObject.parseObject(ticketsTask.getParseOrderParams(), ParseAgvRobotParams.class);
                        robotTransportTaskDto.setStartSite(params.getSourcePosName());
                        robotTransportTaskDto.setTargetSite(params.getTargetPosName());
                    } else if (robot.getRobotModel().equals(RobotModelEnum.PATROL.getFlag()) && StrUtil.isNotBlank(tickets.getContent())) {
                        List<PatrolResultEntity> results = patrolResultService.list(Wrappers.<PatrolResultEntity>lambdaQuery().eq(PatrolResultEntity::getTicketsId, tickets.getId()));
                        int totalCount = results.size();
                        long noCheckCount = results.stream().filter(result -> result.getStatus() == 0).count();
                        robotPatrolTaskDto.setPercent(results.isEmpty() ? 0 : (int) (totalCount - noCheckCount) * 100 / totalCount);
                        if (PatrolConstant.patrolTaskDto != null) {
                            robotPatrolTaskDto.setDevName(PatrolConstant.patrolTaskDto.getDevName());
                            robotPatrolTaskDto.setItemName(PatrolConstant.patrolTaskDto.getItemName());
                            String resultStr = switch (PatrolConstant.patrolTaskDto.getStatus()) {
                                case 1 -> PatrolConstant.patrolTaskDto.getShowValue() + ",正常";
                                case 2 -> PatrolConstant.patrolTaskDto.getShowValue() + ",异常";
                                case 3 -> "识别失败";
                                default -> "";
                            };
                            robotPatrolTaskDto.setResultStr(resultStr);
                            robotPatrolTaskDto.setUrl(StrUtil.isBlank(PatrolConstant.patrolTaskDto.getVisionMediaUrl()) ? com.hitqz.robot.biz.util.PatrolConstant.patrolTaskDto.getMediaUrl() : PatrolConstant.patrolTaskDto.getVisionMediaUrl());
                            robotPatrolTaskDto.setCheckTime(DateUtil.format(PatrolConstant.patrolTaskDto.getCheckTime(), "yyyy-MM-dd HH:mm:ss"));
                        }
                    } else {
                        robotTransportTaskDto.setStartSite("");
                        robotTransportTaskDto.setTargetSite("");
                    }
                }


            } else {
                robotTaskDto.setId("");
                robotTaskDto.setName("");
                robotTaskDto.setType("");
                robotTaskDto.setStatus("");
                robotTaskDto.setStartTime("");
            }//
            Tickets lastTickets = ticketsService.getOne(Wrappers.<Tickets>lambdaQuery().orderByDesc(Tickets::getCreateTime).last("limit 1"));
            if (lastTickets != null) {
                robotTaskDto.setLastTaskStartTime(DateUtil.format(lastTickets.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
                robotTaskDto.setLastTaskEndTime(DateUtil.format(lastTickets.getFinishTime(), "yyyy-MM-dd HH:mm:ss"));
            }
            robotTaskDto.setPatrolTask(robotPatrolTaskDto);
            robotTaskDto.setTransportTask(robotTransportTaskDto);
            dto.setTask(robotTaskDto);
            RobotControlPanelDto robotControlPanelDto = new RobotControlPanelDto();
//            List<RobotControlButtonDto> buttons = Lists.newArrayList();

            // 使用方法
            List<Button> buttonsDb = buttonService.list(Wrappers.<Button>lambdaQuery().eq(Button::getObject, "robot").eq(Button::getIsShow, true));
            List<RobotControlButtonDto> buttons = BeanUtil.copyToList(buttonsDb, RobotControlButtonDto.class);
            robotControlPanelDto.setButtons(buttons);
            dto.setControlPanel(robotControlPanelDto);


            // 机器人报警
            List<RobotWarningDto> robotWarningDtos = Lists.newArrayList();
            List<SysWarning> sysWarnings = sysWarningService.list(Wrappers.<SysWarning>lambdaQuery().orderByDesc(SysWarning::getId)
                    .ne(SysWarning::getCode, "999")
                    .ne(SysWarning::getTitle, "检测告警")
                    .eq(SysWarning::getStatus, 0)
                    .last("limit 10"));
            sysWarnings.stream().filter(t -> t.getLevel() == 0 || t.getLevel() == 1).forEach(t -> {
                RobotWarningDto robotWarningDto = new RobotWarningDto();
                robotWarningDto.setId(t.getId());
                robotWarningDto.setStatus(t.getStatus());
                robotWarningDto.setType("0");
                robotWarningDto.setTitle(t.getTitle());
                robotWarningDto.setContent(t.getContent());
                robotWarningDto.setTime(DateUtil.format(t.getWarningTime(), "yyyy-MM-dd HH:mm:ss"));
                robotWarningDto.setLevel(t.getLevel());
                robotWarningDto.setPath(t.getPaths());
                robotWarningDtos.add(robotWarningDto);
            });
            dto.setWarning(robotWarningDtos);

            //  巡检报警
//            List<SysWarning> errorChecks = sysWarningService.list(Wrappers.<SysWarning>lambdaQuery().orderByDesc(SysWarning::getId)
//                            .eq(SysWarning::getCode, "999")
//                    .last("limit 10"));
//            List<RobotWarningDto> errors = Lists.newArrayList();
//            errorChecks.stream().forEach(t-> {
//                RobotWarningDto robotWarningDto = new RobotWarningDto();
//                robotWarningDto.setType("0");
//                robotWarningDto.setId(t.getId());
//                robotWarningDto.setTitle(t.getTitle());
//                robotWarningDto.setStatus(t.getStatus());
//                robotWarningDto.setContent(t.getContent());
//                robotWarningDto.setTime(DateUtil.format(t.getWarningTime(), "yyyy-MM-dd HH:mm:ss"));
//                robotWarningDto.setLevel(t.getLevel());
//                robotWarningDto.setPath(t.getPaths());
//                errors.add(robotWarningDto);
//            });
//            dto.setErrorChecks(errors);

            // 其他状态信息
            Boolean webRefresh = Optional.ofNullable(robotWebRefreshCache.getIfPresent("webRefresh")).orElse(false);
            RobotOtherInfoDto robotOtherInfoDto = new RobotOtherInfoDto();
            robotOtherInfoDto.setWebRefresh(webRefresh);
            robotOtherInfoDto.setRobotLength(robot.getRobotLength());
            robotOtherInfoDto.setRobotWidth(robot.getRobotWidth());
            robotOtherInfoDto.setRobotAngleLength(robot.getRobotAngleLength());
            dto.setOtherInfo(robotOtherInfoDto);
            List<RobotCustomInfo> robotCustomInfos = robotDriverService.getCustomInfo();
            if (CollUtil.isNotEmpty(robotCustomInfos)) {
                List<RobotCustomInfoDto> robotCustomInfoDtos = Lists.newArrayList();
                List<RobotCustomInfoDto> homeShowInfo = Lists.newArrayList();
                robotCustomInfos.forEach(t -> {
                    robotCustomInfoDtos.add(new RobotCustomInfoDto(t.getLabel(), t.getValue(), t.getKey()));
                    if (t.getValue().contains("LEL") || t.getValue().contains("℃") || t.getValue().contains("%") || t.getValue().contains("ppm")) {
                        homeShowInfo.add(new RobotCustomInfoDto(t.getLabel(), t.getValue(), t.getKey()));
                    }
                });
                robotOtherInfoDto.setCustomInfos(robotCustomInfoDtos);
                dto.setHomeShowInfo(homeShowInfo);
            }

            String data = JSONObject.toJSONString(dto);
            if (log.isDebugEnabled()) {
                log.debug("send data to robot" + data);
            }
            BroadcastTopicUtil.broadcastMessageByTopic(SubscribeTopicEnum.ROBOT_INFO.getTopic(), data);
            // 发送给服务端
            send2ServerService.send2ServerData(Integer.valueOf(ProtocolEnum.ROBOT_SUMMARY.getCmd()), JSONObject.toJSONString(dto));
        }

    }

    /**
     * 报警信息
     */
    @Scheduled(fixedDelay = 3000L)
    public void sendWarning() {
        SysWarning sysWarning = sysWarningService.getOne(Wrappers.<SysWarning>lambdaQuery()
                .eq(SysWarning::getStatus, 0)
                .orderByDesc(SysWarning::getId)
                .last("limit 1"));
        RobotWarningDto robotWarningDto = null;
        if (sysWarning != null) {
            robotWarningDto = new RobotWarningDto();
            robotWarningDto.setId(sysWarning.getId());
            robotWarningDto.setType("0");
            robotWarningDto.setTitle(sysWarning.getTitle());
            robotWarningDto.setContent(sysWarning.getContent());
            robotWarningDto.setTime(DateUtil.format(sysWarning.getWarningTime(), "yyyy-MM-dd HH:mm:ss"));
            robotWarningDto.setLevel(1);
            robotWarningDto.setPath(sysWarning.getPaths());
        } else {
            robotWarningDto = new RobotWarningDto();
            robotWarningDto.setId(-1L);
        }
        BroadcastTopicUtil.broadcastMessageByTopic(SubscribeTopicEnum.ROBOT_WARNING.getTopic(), JSONObject.toJSONString(robotWarningDto));
    }


    /**
     * 挂轨--语音对讲-进行页面通知
     */
//    @Scheduled(fixedDelay = 3000L)
    public void sendSpeak() {
        R result = robotDriverService.executeOtherCommand(RobotOtherCommandEnum.GET_SPEAK_REQUEST, "");
        BroadcastTopicUtil.broadcastMessageByTopic(SubscribeTopicEnum.ROBOT_SPEAK_REQUEST.getTopic(), JSONObject.toJSONString(result));
    }


    private RobotMapPos findNearestPosition(IRobotPosDto posDto, List<RobotMapPos> mapPositions) {
        return this.nearPos(posDto.getX(), posDto.getY(), mapPositions);
    }

    private RobotMapPos nearPos(float x, float y, List<RobotMapPos> robotMapPosList) throws IllegalStateException {
        if (robotMapPosList.isEmpty()) {
            throw new IllegalArgumentException("输入的列表为空");
        }

        GeometryFactory geometryFactory = new GeometryFactory();
        RobotMapPos nearestPos = null;
        double distance = Double.MAX_VALUE;
        boolean foundValidPosition = false;
        Point point = geometryFactory.createPoint(new Coordinate(x, y));

        for (RobotMapPos pos : robotMapPosList) {
            Point targetPos = geometryFactory.createPoint(new Coordinate(pos.getPosX(), pos.getPosY()));
            double currentDistance = DistanceOp.distance(point, targetPos);
            if (currentDistance < distance) {
                distance = currentDistance;
                nearestPos = pos;
                foundValidPosition = true;
            }
        }

        if (!foundValidPosition) {
            throw new IllegalStateException("没有找到有效的点位，所有距离均超过阈值。");
        }
        return nearestPos;
    }


//    @Scheduled(fixedDelay = 2000L)
//    public void sendEquipmentStatus(){
//        List<RobotEquipment> robotEquipments = robotEquipmentService.list();
//        for (RobotEquipment robotEquipment : robotEquipments) {
//            IEquipmentService service = equipmentFactory.getServiceByBrand(robotEquipment.getBrand());
//            QueryParamsDto queryParamsDto = new QueryParamsDto();
//            queryParamsDto.setUsername(robotEquipment.getUsername());
//            queryParamsDto.setPassword(robotEquipment.getPassword());
//            queryParamsDto.setPort(robotEquipment.getPort());
//            queryParamsDto.setIp(robotEquipment.getIp());
//            queryParamsDto.setChannel(robotEquipment.getChannel());
//            if (!NetUtil.ping(robotEquipment.getIp())){
//                continue;
//            }
//            if (service != null) {
//                BroadcastTopicUtil.broadcastMessageByRandomId(
//                        robotEquipment.getId().intValue(), SubscribeTopicEnum.ROBOT_EQUIPMENT_STATUS,
//                        JSONObject.toJSONString(service.getEquipmentParams(queryParamsDto)));
//            }
//        }
//
//    }


}
